package lesson6;

//Person想要能够被克隆
//1、实现Cloneable
//2、重写Object中的clone方法
public class Driver implements Cloneable{
    private String name;
    private int age;

    private Car car;

    public Driver() {
    }

    public Driver(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Driver(String name, int age,Car car) {
        this.name = name;
        this.age = age;
        this.car = car;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        //return super.clone();
        Driver driver = (Driver) super.clone();
        Car newCar = (Car) driver.car.clone();
        driver.setCar(newCar);

        return driver;
    }

    public Car getCar() {
        return car;
    }

    public void setCar(Car car) {
        this.car = car;
    }


    @Override
    public String toString() {
        return "Driver{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", car=" + car +
                '}';
    }
}
